//统一管理咱们项目用户相关的接口
import request from '@/utils/request'
import { AxiosResponse } from 'axios'
import qs from 'qs'
import { OptionType } from '../user/type'
import {
  CoursePageQuery,
  CoursePageRes,
  CourseParams,
  CourseType,
  SelectCourseParams,
  WithdrawCourseParams,
} from './type'

// 项目用户相关的请求地址
enum Course_API {
  ADMIN_GET_COURSE_PAGE = '/teaching_system/course/more',
  STU_GET_COURSE_PAGE = '/teaching_system/course/more/stu',
  TEA_GET_COURSE_PAGE = '/teaching_system/course/more/tea',
  POST_PUT_COURSE_ONE = '/teaching_system/course/one',
  DELETE_COURSE_BYID = '/teaching_system/course/one/{id}',
  POST_PUT_SELECT_COURSE_BYADMIN = '/teaching_system/course/select',
  POST_PUT_SELECT_COURSE_BYSTU = '/teaching_system/course/select/stu',
  GET_OPTIONAL_COURSE_BYSTU = '/teaching_system/course/option/{semesterId}',
  GET_TEACHER_OPTIONS = '/teaching_system/course/tea/option',
}

/**
 * 管理员获取课程
 * @param {string} CoursePageQuery
 * @returns
 */
export const reqGetAllCourseByAdmin = (params: CoursePageQuery) =>
  request.get<any, AxiosResponse<CoursePageRes>>(
    Course_API.ADMIN_GET_COURSE_PAGE + '?' + qs.stringify(params),
  )

/**
 * 学生获取课程
 * @param {string} CoursePageQuery
 * @returns
 */
export const reqGetCourseByStu = (params: CoursePageQuery) =>
  request.get<any, AxiosResponse<CoursePageRes>>(
    Course_API.STU_GET_COURSE_PAGE + '?' + qs.stringify(params),
  )

/**
 * 老师获取课程
 * @param {string} CoursePageQuery
 * @returns
 */
export const reqGetCourseByTea = (params: CoursePageQuery) =>
  request.get<any, AxiosResponse<CoursePageRes>>(
    Course_API.TEA_GET_COURSE_PAGE + '?' + qs.stringify(params),
  )

/**
 * 添加课程
 * @param {object} params 课程添加表
 * @param {string} params.courseName 课程名称
 * @param {string} params.courseCode 课程代码
 * @param {number} params.teacherId 课程名称
 * @param {object} params.credit 学分名称
 * @param {string} params.department 开设院系
 * @param {string} params.description 课程描述
 * @param {number} params.maxNum 可选人数
 * @param {number} params.semesterId 学期
 * @returns
 */
export const reqAddCourse = (params: CourseParams) =>
  request.post<any, AxiosResponse<any>>(Course_API.POST_PUT_COURSE_ONE, params)

/**
 * 修改课程
 * @param {object} params 课程添加表
 * @param {number} params.courseId 课程courseId
 * @param {string} params.courseName 课程名称
 * @param {string} params.courseCode 课程代码
 * @param {number} params.teacherId 课程名称
 * @param {object} params.credit 学分名称
 * @param {string} params.department 开设院系
 * @param {string} params.description 课程描述
 * @param {number} params.maxNum 可选人数
 * @param {number} params.semesterId 学期
 * @returns
 */
export const reqUpdateCourse = (params: CourseParams) =>
  request.put<any, AxiosResponse<any>>(Course_API.POST_PUT_COURSE_ONE, params)

/**
 * 根据id删除课程
 * @param {string} id
 * @returns
 */
export const reqDeleteCourseById = (id: number) =>
  request.delete<any, AxiosResponse<any>>(
    Course_API.DELETE_COURSE_BYID.replace('{id}', id.toString()),
  )

/**
 * 选课
 * @param {object} params 选课表
 * @param {number} params.studentId 学生ID
 * @param {array} params.courseIds 课程IDS
 * @returns
 */
export const reqAddEnrollment = (params: SelectCourseParams) =>
  request.post<any, AxiosResponse<any>>(
    Course_API.POST_PUT_SELECT_COURSE_BYADMIN,
    params,
  )

/**
 * 退课
 * @param {object} params 删除选课表
 * @param {array} params.enrollmentIds 课程IDS
 * @returns
 */
export const reqDeleteEnrollment = (params: WithdrawCourseParams) =>
  request.put<any, AxiosResponse<any>>(
    Course_API.POST_PUT_SELECT_COURSE_BYADMIN,
    params,
  )

/**
 * 学生选课
 * @param {object} params 选课表
 * @param {number} params.studentId 学生ID
 * @param {array} params.courseIds 课程IDS
 * @returns
 */
export const reqAddEnrollmentByStu = (params: SelectCourseParams) =>
  request.post<any, AxiosResponse<any>>(
    Course_API.POST_PUT_SELECT_COURSE_BYSTU,
    params,
  )

/**
 * 学生退课
 * @param {object} params 删除选课表
 * @param {array} params.enrollmentIds 课程IDS
 * @returns
 */
export const reqDeleteEnrollmentByStu = (params: WithdrawCourseParams) =>
  request.put<any, AxiosResponse<any>>(
    Course_API.POST_PUT_SELECT_COURSE_BYSTU,
    params,
  )

/**
 * 教师选项
 * @returns
 */
export const reqGetTeacherOption = () =>
  request.get<any, AxiosResponse<OptionType[]>>(Course_API.GET_TEACHER_OPTIONS)

/**
 * 学生获取可选课程
 * @param {string} semesterId
 * @returns
 */
export const reqGetOptionalCourseByStu = (semesterId: number) =>
  request.get<any, AxiosResponse<CourseType[]>>(
    Course_API.GET_OPTIONAL_COURSE_BYSTU.replace(
      '{semesterId}',
      semesterId.toString(),
    ),
  )
